package com.enqualcomm.kids.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.enqualcomm.kids.R;

import common.utils.DensityUtil;

public class MyMultiDialog extends Dialog {
	private MultiDialogCallBack callBack;
//	private SpannableStringBuilder style;
	private String title;
	private int btnCount;
//	/**
//	 * 
//	 * @param context
//	 * @param style
//	 * 对话框标题
//	 * @param callBack
//	 * 点击确定后的回调接口
//	 * @param btnCount
//	 * 按钮个数，1到3个
//	 */
//	public MyMultiDialog(Context context,SpannableStringBuilder style,MultiDialogCallBack callBack,int btnCount) {
//		super(context);
//		this.callBack = callBack;
//		this.style = style;
//		this.btnCount = btnCount;
//	}
	public MyMultiDialog(Context context, String title, MultiDialogCallBack callBack, int btnCount) {
		super(context);
		this.callBack = callBack;
		this.title = title;
		this.btnCount = btnCount;
	}
	private String name1;
	private String name2;
	private String name3;
	public void setBtnName(String name1,String name2,String name3){
		this.name1 = name1;
		this.name2 = name2;
		this.name3 = name3;
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.dialog_multi);

		setDialogAttributes();
		initDialogView();
	}

	public void setDialogAttributes() {
		Window dialogWindow = getWindow();
		dialogWindow.setGravity(Gravity.CENTER);
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();

		DisplayMetrics dm = new DisplayMetrics();
		dialogWindow.getWindowManager().getDefaultDisplay().getMetrics(dm);

		lp.width = dm.widthPixels - DensityUtil.dip2px(getContext(), 30); // 宽度
		// lp.alpha = 0.9f; // 透明度

		dialogWindow.setAttributes(lp);
		dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
	}

	public void initDialogView() {
		TextView myTerminal = (TextView) findViewById(R.id.myTerminal);
		myTerminal.setText(title);
//		if (title!=null) {
//			myTerminal.setText(title);
//		}else {
//			myTerminal.setText(style);
//		}
		
		findViewById(R.id.dialog_dismiss).setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View arg0, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					dismiss();
				}
				return false;
			}
		});
		Button btn1 = (Button) findViewById(R.id.dialog_btn1);
		btn1.setText(name1);
		btn1.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View arg0, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					callBack.onClick1();
					dismiss();
				}
				return false;
			}
		});
		if (btnCount==1) {
			return;
		}
		Button btn2 = (Button) findViewById(R.id.dialog_btn2);
		btn2.setVisibility(View.VISIBLE);
		btn2.setText(name2);
		btn2.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View arg0, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					callBack.onClick2();
					dismiss();
				}
				return false;
			}
		});
		if (btnCount==2) {
			return;
		}
		Button btn3 = (Button) findViewById(R.id.dialog_btn3);
		btn3.setVisibility(View.VISIBLE);
		btn3.setText(name3);
		btn3.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View arg0, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					callBack.onClick3();
					dismiss();
				}
				return false;
			}
		});
	}

	public interface MultiDialogCallBack {
		void onClick1();
		void onClick2();
		void onClick3();
	}

}
